/* $RCSFile: PagesLayoutManager.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/Design/PagesLayoutManager.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:28  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Serialization;
using System.Text;
using System.IO;

using AddonNice.Configuration;
using AddonNice.Diagnostics;

namespace AddonNice.Design
{
    /// <summary>
    /// The PagesLayoutManager class encapsulates all data logic necessary to
    /// use differents Layouts across the entire portal.
    /// Manages the Load and Save of the Layouts.
    /// Encapsulates a Layout object that contains all the settings
    /// of the current Layout.
    /// </summary>
	public class PagesLayoutManager
    {
        string _portalPath							=	string.Empty;

        public PagesLayoutManager(string portalPath)
        {
            _portalPath = portalPath;
        }

		public void ClearCacheList()
		{
			//Clear cache
			lock (typeof(PagesLayoutManager))
			{
				AddonNice.Settings.Cache.CurrentCache.Remove(AddonNice.Settings.Cache.Key.LayoutList(Path));
				AddonNice.Settings.Cache.CurrentCache.Remove(AddonNice.Settings.Cache.Key.LayoutList(PortalLayoutPath));
			}
		}

		static string innerGlobalLayoutsWebPath					=	string.Empty;

        [System.Obsolete]
        public static string WebPath
        {
            get
            {
                return GlobalLayoutsWebPath;
            }
        }
		/// <summary>
		/// The path of the default Layout dir (Web side)
        /// used to reference layouts as ascx files for main then header,ccontent, footer
        /// Default value : '/Design/PagesLayouts'
        /// </summary>
        public static string GlobalLayoutsWebPath
        {
            get
            {
				if(innerGlobalLayoutsWebPath == string.Empty)
					innerGlobalLayoutsWebPath = AddonNice.Settings.Path.WebPathCombine(AddonNice.Settings.Path.ApplicationRoot,StStr.PagesLayoutGlobalFolder);
				return innerGlobalLayoutsWebPath;
			}
        }
          
        /// <summary>
        /// The path of the current portal Layout dir (Web side)
        /// used to reference layouts as ascx files for main then header,ccontent, footer
        /// Default value : PortalPath+'/PagesLayouts'
        /// </summary>
        public string PortalLayoutsWebPath
        {
			get
			{
				return AddonNice.Settings.Path.WebPathCombine(AddonNice.Settings.Path.ApplicationRoot, _portalPath,StStr.PagesLayoutPrivateFolder);
			}
        }

		/// <summary>
		/// The path of the Global site Layout dir (Physical path) used to load Layouts
        /// correspond to virtual folder GlobalLayoutsWebPath
		/// </summary>
		public static string Path
        {
            get
            {
                return(HttpContext.Current.Server.MapPath(GlobalLayoutsWebPath));
            }
        }

        /// <summary>
        /// The physical path of the current portal Layout dir (Physical path) used to load Layouts
        /// correspond to virtual folder PortalLayoutsWebPath
        /// </summary>
        public string PortalLayoutPath
        {
            get
            {
                return HttpContext.Current.Server.MapPath(PortalLayoutsWebPath);
            }
        }

		/// <summary>
		/// Read the Path dir and returns
		/// an ArrayList with all the Layouts found, public and privates
		/// </summary>
		/// <returns></returns>
		public ArrayList GetLayouts()
		{
			ArrayList layoutList        =   (ArrayList) GetPublicLayouts().Clone();
			ArrayList layoutListPrivate =   GetPrivateLayouts();

			layoutList.AddRange(layoutListPrivate);

			return layoutList;
		}

		/// <summary>
		/// Read the Path dir and returns an ArrayList with all the Layouts found.
		/// Static because the list is Always the same.
		/// </summary>
		/// <returns></returns>
		public static ArrayList GetPublicLayouts()
		{
			ArrayList baseLayoutList;

			if ( !AddonNice.Settings.Cache.CurrentCache.Exists(AddonNice.Settings.Cache.Key.LayoutList(Path)) )
			{
				// initialize array
				baseLayoutList = new ArrayList();
				string[] layouts;

				// Try to read directories from public Layout path
				if (Directory.Exists(Path))
				{
					layouts = Directory.GetDirectories(Path);
				}
				else
				{
					layouts = new string[0];
				}
                int len         =   Path.Length + 1;
				for (int i = 0; i < layouts.Length; i++)
				{
					string nm   =   layouts[i].Substring(len);
					if( nm.Equals("CVS") ) //Ignore CVS
                        continue;
					LayoutItem t = new LayoutItem(nm,ItemOrigin.Public);
					baseLayoutList.Add(t);
				}
				AddonNice.Settings.Cache.CurrentCache.InsertLong(AddonNice.Settings.Cache.Key.LayoutList(Path), baseLayoutList, new CacheDependency(Path));
			}
			else
			{
				baseLayoutList = (ArrayList) AddonNice.Settings.Cache.CurrentCache.Get(AddonNice.Settings.Cache.Key.LayoutList(Path));
			}
			return baseLayoutList;
		}
  
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public ArrayList GetPrivateLayouts()
		{
			ArrayList privateLayoutList;

			if ( !AddonNice.Settings.Cache.CurrentCache.Exists(AddonNice.Settings.Cache.Key.LayoutList(this.PortalLayoutPath)) )
			{

				privateLayoutList = new ArrayList();
				string[] layouts;

				// Try to read directories from private theme path
				if (Directory.Exists(PortalLayoutPath))
				{
					layouts = Directory.GetDirectories(PortalLayoutPath);
				}

				else
				{
					layouts = new string[0];
				}

                int len         =   PortalLayoutPath.Length + 1;
				for (int i = 0; i < layouts.Length; i++)
				{
					string nm   =   layouts[i].Substring(len);
					if( nm.Equals("CVS") ) //Ignore CVS
                        continue;
					LayoutItem t = new LayoutItem(nm,ItemOrigin.Portal);
					privateLayoutList.Add(t);
				}
				AddonNice.Settings.Cache.CurrentCache.InsertLong(AddonNice.Settings.Cache.Key.LayoutList(PortalLayoutPath), privateLayoutList, new CacheDependency(PortalLayoutPath));
			}
			else
			{
				privateLayoutList = (ArrayList) AddonNice.Settings.Cache.CurrentCache.Get(AddonNice.Settings.Cache.Key.LayoutList(PortalLayoutPath));
			}
			return privateLayoutList;
		}
	}
}